package com.cn.wanxi.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.service.ProductService;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/product/edit")
public class ProductEditServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        1.处理乱码
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的数据
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String normalPrice = req.getParameter("normalPrice");
        String marketPrice = req.getParameter("marketPrice");
        String cuisineId = req.getParameter("cuisineId");
        String isRecommend = req.getParameter("isRecommend");
        String isTop = req.getParameter("isTop");


//        3.封装model;
        ProductModel model = new ProductModel();
        model.setId(Tool.stringToInt(id));
        model.setName(name);
        model.setNormalPrice(Tool.stringToMoney(normalPrice));
        model.setMarketPrice(Tool.stringToMoney(marketPrice));
        model.setCuisineId(Tool.stringToInt(cuisineId));
        model.setIsRecommend(Tool.stringToInt(isRecommend));
        model.setIsTop(Tool.stringToInt(isTop));
//        4.调用逻辑层
        ProductService productService = new ProductServiceImpl();
//        5.得到返回结果
        ResultModel resultModel = productService.update(model);
//        6.返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultModel));

    }
}
